#ifndef __FLASH_DRIVER_H__
#define __FLASH_DRIVER_H__

#include "main.h"

#include "stdio.h"
#include "string.h"
#include <stdint.h>
#include <stdbool.h>

#include "stm32f4xx_hal.h"

#define FLASH_FLAG_ADDR  	 0x080E0000


// FLASH相关定义
#define FLASH_START_ADDR        0x08000000U    // FLASH起始地址
#define FLASH_END_ADDR          0x080FFFFFU    // FLASH结束地址
#define FLASH_SECTOR_SIZE       0x4000U        // 扇区大小(16KB)
#define FLASH_PAGE_SIZE         4U             // FLASH编程页大小(4字节)

// 扇区大小定义
#define SECTOR_0_3_SIZE         (16 * 1024)    // 扇区0-3: 16KB
#define SECTOR_4_SIZE           (64 * 1024)    // 扇区4: 64KB
#define SECTOR_5_11_SIZE        (128 * 1024)   // 扇区5-11: 128KB

// 扇区边界定义
#define SECTOR_0_START          0x08000000U
#define SECTOR_1_START          0x08004000U
#define SECTOR_2_START          0x08008000U
#define SECTOR_3_START          0x0800C000U
#define SECTOR_4_START          0x08010000U
#define SECTOR_5_START          0x08020000U
#define SECTOR_6_START          0x08040000U
#define SECTOR_7_START          0x08060000U
#define SECTOR_8_START          0x08080000U
#define SECTOR_9_START          0x080A0000U
#define SECTOR_10_START         0x080C0000U
#define SECTOR_11_START         0x080E0000U

// 函数声明
HAL_StatusTypeDef FLASH_Read(uint8_t* data, uint32_t len, uint32_t addr);
HAL_StatusTypeDef FLASH_Write_NoErase(uint8_t* data, uint32_t len, uint32_t addr);
HAL_StatusTypeDef FLASH_Erase(uint32_t addr, uint32_t len);
uint32_t FLASH_GetAlignedLength(uint32_t len);
uint32_t FLASH_GetSector(uint32_t address);

#endif
